<?php
namespace app\modules\admin\controllers;
use app\modules\admin\models\AdminUser;
use app\modules\admin\models\Config;
use yii\web\Controller;
use Yii;

class LoginController extends Controller{

    public function actions()
    {
        return [
            'captcha'=>[
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,//调整验证码是数字还是中文
                'backColor'=>0xf5f5f5,//背景颜色
                'maxLength' => 6, //最大显示个数
                'minLength' => 4,//最少显示个数
                'padding' => 3,//间距
                'height'=>50,//高度
                'width' => 137,  //宽度
                'foreColor'=>0x8f8f8f,     //字体颜色
                'offset'=>4,        //设置字符偏移量 有效果
            ],
        ];
    }

    public function actionLogin(){
        //管理层登录方法
        $model = new AdminUser();
        $model->scenario = 'login';
        $data = Yii::$app->request;
        if($data->isPost){
            $post = $data->post('AdminUser');
            $DM = AdminUser::find()->where(['email'=>$post['email']])->one();
            if(!is_null($DM) and $DM->status==1 and $post['password']==$DM->password){
                $session = Yii::$app->session;
                $session['Admin'] = [
                    'token' => base64_encode($post['email']),
                    'name' => $DM->name,
                ];
                $this->redirect('index.php?r=admin');
            }
            else{
                echo "<script>alert('输入的邮箱或密码不正确！');location.href='index.php?r=admin/login/login'</script>";
            }
        }
        return $this->renderPartial('login',['model'=>$model]);
    }

    public function actionLogOut(){
        //退出登陆
        unset(Yii::$app->session['Admin']);
        return $this->redirect('index.php?r=admin');
    }

    public function actionForget(){
        //管理员忘记密码找回
        $model = new AdminUser();
        $model->scenario = 'forget';
        $data = Yii::$app->request;
        if($data->isPost){
            $post = $data->post('AdminUser');
            $DM = AdminUser::find()->where(['email'=>$post['email']])->one();
            if(count($DM)=='1')
            {
                $DM->name = $post['name'];
                $DM->email = $post['email'];
                $DM->password = $post['password'];
                $DM->status = 0;
                $DM->save(false);
                $this->SendEmail($post['email']);    //发送邮件
                return $this->renderPartial('login',['model'=>$model]);
            }
            else{
                echo "<script>alert('该邮箱尚未注册，请检查你的邮箱');location.href='index.php?r=admin/login/forget'</script>";
                return $this->renderPartial('forget',['model'=>$model]);
            }
        }
        return $this->renderPartial("forget",['model'=>$model]);
    }

    public function SendEmail($email)
    {

        //抽取邮箱验证地址
        $url = Config::findOne(1);
        $url = $url->web_url;

        $token = crypt($email, md5($email));
        $validate = base64_encode($email);
        //邮箱发送
        $mail= Yii::$app->mailer->compose();
        $mail->setTo($email);
        $mail->setSubject("激活邮箱");
        //发布可以带html标签的文本
        $mail->setHtmlBody("爱未来管理员账号激活链接 <a href='".$url."/index.php?r=admin/login/live&token=".$token."&validate=".$validate."'>点击链接激活邮箱</a>");
        if($mail->send())
            echo "<script>alert('激活邮件发送至你的邮箱，注意查收！');location.href='index.php?r=admin/login/login'</script>";
        else
            echo "<script>alert('激活邮件发送失败，请重试！');location.href='index.php?r=admin/login/login'</script>";
        die();
    }

    public function actionLive()
    {
        $email=base64_decode(Yii::$app->request->get('validate'));
        $token=Yii::$app->request->get('token');
        $token_check=crypt($email,md5($email));
        if($token_check==$token)
        {
            $DM = AdminUser::find()->where(['email'=>$email])->one();
            $DM->status = 1;
            $data = $DM->save(false);
            if($data)
            {
                echo "<script>alert('激活成功，可登录');location.href='index.php?r=admin/login/login'</script>";
            }
            else
            {
                echo "<script>alert('激活失败');location.href='index.php?r=admin/login/forget'</script>";
            }
        }
        else
        {
            echo "<script>alert('参数错误,重新激活');location.href='index.php?r=admin/login/forget'</script>";
        }
    }

}